package org.jeecg.glr.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.glr.entity.GlrSensorData;
import org.jeecg.glr.entity.GlrVideo;
import org.jeecg.glr.mapper.GlrSensorDataMapper;
import org.jeecg.glr.mapper.GlrVideoMapper;
import org.jeecg.glr.service.IGlrSensorDataService;
import org.jeecg.glr.service.IGlrVideoService;
import org.jeecg.glr.util.StringUtil;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 传感器数据
 * @Author: jeecg-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@Service
public class GlrSensorDataServiceImpl extends ServiceImpl<GlrSensorDataMapper, GlrSensorData> implements IGlrSensorDataService {

    @Resource
    private SysDictMapper sysDictMapper;
    /**
     * 通过查询指定code 获取字典
     * @param code
     * @return
     */
    @Cacheable(value = CacheConstant.SYS_DICT_CACHE,key = "#code")
    public List<DictModel> queryDictItemsByCode(String code) {
        return sysDictMapper.queryDictItemsByCode(code);
    }

    /**
     *
     * <p>
     * 国际化
     * 获取风向
     * </p>
     *
     * @param windDirection
     * @return
     * @author:Administrator 2019年6月5日
     * @update: [updatedate] [changer][change description]
     */
    public String getWindDirection(String windDirection){
        if(StringUtil.isEmpty(windDirection))
        {
            return null;
        }
        String windDirection_code="";
        int num = Integer.parseInt(windDirection);
        if(num == 0)
        {
            //return "北风";
            windDirection_code="0";
        }
        else if(num > 0 && num < 90)
        {
            //return "东北风";
            windDirection_code="1";
        }
        else if(num == 90)
        {
            //return "东风";
            windDirection_code="2";
        }
        else if(num > 90 && num < 180)
        {
            //return "东南风";
            windDirection_code="3";
        }
        else if(num == 180)
        {
            //return "南风";
            windDirection_code="4";
        }
        else if(num > 180 && num < 270)
        {
            //return "西南风";
            windDirection_code="5";
        }
        else if(num == 270)
        {
            //return "西风";
            windDirection_code="6";
        }
        else if(num > 270 && num < 359)
        {
            //return "西北风";
            windDirection_code="7";
        }
        if(StringUtil.isEmpty(windDirection_code))
        {
            return null;
        }
        List<DictModel> types = queryDictItemsByCode("wind".toLowerCase());
        String windDirection_name="";
        for (DictModel type : types) {
            if (type.getValue().equals(windDirection_code)){
                windDirection_name= type.getText();
                break;
            }
        }
        return windDirection_name;
    }

    private double getDeouble(Object o){
        if (StringUtil.isNotEmpty(o)){
            return new BigDecimal(Double.parseDouble(o.toString())).setScale(3, BigDecimal.ROUND_HALF_DOWN).doubleValue();
        }else{
            return 0d;
        }
    }

    @Override
    public void doComputeYesterdayData(String stDate){
        if (stDate.length()!=10){
            return;
        }
        String stsql="SELECT MIN(id) AS id,sensor_code,count(*) as count,\n" +
                "max(rainfall) as rainfall_max,min(rainfall) as rainfall_min,avg(rainfall) as rainfall_avg,\n" +
                "max(humidity) as humidity_max,min(humidity) as humidity_min,avg(humidity) as humidity_avg,\n" +
                "max(airpressure) as airpressure_max,min(airpressure) as airpressure_min,avg(airpressure) as airpressure_avg,\n" +
                "max(wind_speed) as wind_speed_max,min(wind_speed) as wind_speed_min,avg(wind_speed) as wind_speed_avg,\n" +
                "max(temperature) as temperature_max,min(temperature) as temperature_min,avg(temperature) as temperature_avg,\n" +
                "max(optical_radiation) as optical_radiation_max,min(optical_radiation) as optical_radiation_min,avg(optical_radiation) as optical_radiation_avg,\n" +
                "max(ultraviolet_ray) as ultraviolet_ray_max,min(ultraviolet_ray) as ultraviolet_ray_min,avg(ultraviolet_ray) as ultraviolet_ray_avg,\n" +
                "max(noise) as noise_max,min(noise) as noise_min,avg(noise) as noise_avg,\n" +
                "max(pm25) as pm25_max,min(pm25) as pm25_min,avg(pm25) as pm25_avg,\n" +
                "max(pm10) as pm10_max,min(pm10) as pm10_min,avg(pm10) as pm10_avg\n" +
                "FROM glr_sensor_data where date_format(create_time,'%Y-%m-%d') = '"+stDate+"' GROUP BY sensor_code";

        List<Map<String, Object>> sqlList=baseMapper.getListBySql(stsql);

        for(Map<String, Object> map:sqlList){
            String id=map.get("id").toString();

            Integer count=Integer.parseInt(map.get("count").toString());

            if (count>1){

                String StUpdateSql="update glr_sensor_data set \n " +
                        "rainfall_max="+getDeouble(map.get("rainfall_max"))+",\n " +
                        "rainfall_min="+getDeouble(map.get("rainfall_min"))+",\n " +
                        "rainfall_avg="+getDeouble(map.get("rainfall_avg"))+",\n " +

                        "humidity_max="+getDeouble(map.get("humidity_max"))+",\n " +
                        "humidity_min="+getDeouble(map.get("humidity_min"))+",\n " +
                        "humidity_avg="+getDeouble(map.get("humidity_avg"))+",\n " +

                        "airpressure_max="+getDeouble(map.get("airpressure_max"))+",\n " +
                        "airpressure_min="+getDeouble(map.get("airpressure_min"))+",\n " +
                        "airpressure_avg="+getDeouble(map.get("airpressure_avg"))+",\n " +

                        "wind_speed_max="+getDeouble(map.get("wind_speed_max"))+",\n " +
                        "wind_speed_min="+getDeouble(map.get("wind_speed_min"))+",\n " +
                        "wind_speed_avg="+getDeouble(map.get("wind_speed_avg"))+",\n " +

                        "temperature_max="+getDeouble(map.get("temperature_max"))+",\n " +
                        "temperature_min="+getDeouble(map.get("temperature_min"))+",\n " +
                        "temperature_avg="+getDeouble(map.get("temperature_avg"))+",\n " +

                        "optical_radiation_max="+getDeouble(map.get("optical_radiation_max"))+",\n " +
                        "optical_radiation_min="+getDeouble(map.get("optical_radiation_min"))+",\n " +
                        "optical_radiation_avg="+getDeouble(map.get("optical_radiation_avg"))+",\n " +

                        "ultraviolet_ray_max='"+getDeouble(map.get("ultraviolet_ray_max"))+"',\n " +
                        "ultraviolet_ray_min='"+getDeouble(map.get("ultraviolet_ray_min"))+"',\n " +
                        "ultraviolet_ray_avg='"+getDeouble(map.get("ultraviolet_ray_avg"))+"',\n " +

                        "noise_max="+getDeouble(map.get("noise_max"))+",\n " +
                        "noise_min="+getDeouble(map.get("noise_min"))+",\n " +
                        "noise_avg="+getDeouble(map.get("noise_avg"))+",\n " +

                        "pm25_max="+getDeouble(map.get("pm25_max"))+",\n " +
                        "pm25_min="+getDeouble(map.get("pm25_min"))+",\n " +
                        "pm25_avg="+getDeouble(map.get("pm25_avg"))+",\n " +

                        "pm10_max="+getDeouble(map.get("pm10_max"))+",\n " +
                        "pm10_min="+getDeouble(map.get("pm10_min"))+",\n " +
                        "pm10_avg="+getDeouble(map.get("pm10_avg"))+"\n " +

                        "where id='"+id+"'";

                baseMapper.updateSql(StUpdateSql);
            }
        }

        String deleteSql="delete from  glr_sensor_data where id not in (SELECT id FROM(SELECT MIN(id) AS id FROM glr_sensor_data where DATE_FORMAT(create_time,'%Y-%m-%d') = '"+stDate+"' GROUP BY sensor_code ) dt) and  DATE_FORMAT(create_time,'%Y-%m-%d')='"+stDate+"'";
        baseMapper.deleteSql(deleteSql);

    }


}
